package polyu.comp;

import android.app.AlertDialog;
import android.content.*;
import android.net.*;
import android.net.wifi.WifiManager;
import android.util.Log;

/***
 * 
 * This Module is used to listen the network status, if get connected, try to sync with sever.
 *
 */
public class NetCheckReceiver extends BroadcastReceiver {

	private static final String netACTION = "android.net.conn.CONNECTIVITY_CHANGE";
    
	@Override
    public void onReceive(Context context, Intent intent) {
		
		if(intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
		    NetworkInfo networkInfo = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
		    if(networkInfo.isConnected()) {
		        // Wifi is connected
		    	Log.v(this.getClass().getName(), "Connected");
		    	 
		    	Intent trIntent = new Intent("android.intent.action.MAIN");
				trIntent.setClass(context, DialogActivity.class);
				trIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				context.startActivity(trIntent);
		    }
		} 

		/*
        if(intent.getAction().equals(netACTION)){
        	
        	if (!DataManager.instance().isConnected())
        		return ;

            if(intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false)){
            	Log.v(this.getClass().getName(), "Disconnected");
            }else{
                Log.v(this.getClass().getName(), "Connected");
                
                // must pass application context
				Intent trIntent = new Intent("android.intent.action.MAIN");
				trIntent.setClass(context, DialogActivity.class);
				trIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				context.startActivity(trIntent);
            }
        }
        */
    }
	

}
